package middle;

import com.eclipsesource.json.JsonArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 542. 01 矩阵
 */
public class UpdateMatrix {


    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for (int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static int[][] stringToInt2dArray(String input) {
        JsonArray jsonArray = JsonArray.readFrom(input);
        if (jsonArray.size() == 0) {
            return new int[0][0];
        }

        int[][] arr = new int[jsonArray.size()][];
        for (int i = 0; i < arr.length; i++) {
            JsonArray cols = jsonArray.get(i).asArray();
            arr[i] = stringToIntegerArray(cols.toString());
        }
        return arr;
    }

    public static String int2dArrayToString(int[][] array) {
        if (array == null) {
            return "null";
        }
        if (array.length == 0) {
            return "[]";
        }

        StringBuilder sb = new StringBuilder("[");
        for (int[] item : array) {
            sb.append(Arrays.toString(item));
            sb.append(",");
        }

        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[][] mat = stringToInt2dArray(line);

            int[][] ret = new Solution().updateMatrix(mat);

            String out = int2dArrayToString(ret);

            System.out.print(out);
        }
    }

    static class Solution {
        static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

        public int[][] updateMatrix(int[][] mat) {
            int m = mat.length,n=mat[0].length;
            int[][] dist = new int[m][n];
            Queue<int[]> queue = new LinkedBlockingQueue<>();
            for (int i = 0; i < mat.length; i++) {
                for (int j = 0; j < mat[0].length; j++) {
                    if (mat[i][j] == 0) {
                        dist[i][j] = 0;
                        queue.offer(new int[]{i,j});
                    }
                }
            }
            while (!queue.isEmpty()){
                int[] point = queue.poll();
                for (int i=0;i<4;i++){
                    int ni = dirs[i][0]+point[0];
                    int nj = dirs[i][1]+point[1];
                    if (ni>=0&&ni<m&&nj>=0&&nj<n&&mat[ni][nj]==1){
                        if (dist[ni][nj]==0){
                            dist[ni][nj] = dist[point[0]][point[1]] + 1;
                            queue.offer(new int[]{ni,nj});
                        }else if (dist[ni][nj] > dist[point[0]][point[1]] + 1){
                            dist[ni][nj] = dist[point[0]][point[1]] + 1;
                        }
                    }
                }
            }
            return dist;
        }
    }
}
